<?php defined('SYSPATH') or die('No direct script access.');
	
	class Model_Services_Comment extends Model implements Jqgrid_GridListener{
	
		public $comment;
		
		public function __construct($id=false){
			if ($id) $this->comment = $this->get($id);
		}
		
		public function count($criteria = false){ return ORM::factory("dao_comment")->count_all();}		
		public function getList($criteria = false){
			
			if (!$criteria)	$comment_list = ORM::factory("dao_comment")->find_all()->as_array();
			else{
				if ($criteria->isSearch){
					$comment_list = ORM::factory("dao_comment")
									->where($criteria->getSQLField("comments"),$criteria->getSQLOper(), $criteria->getSQLSearchString())
									->order_by($criteria->sidx,$criteria->sord)
									->limit($criteria->limit)
									->offset($criteria->calcOffset())
									->find_all()
									->as_array();
							
				}else{
					$comment_list = ORM::factory("dao_comment")->order_by($criteria->sidx,$criteria->sord)->limit($criteria->limit)->offset($criteria->calcOffset())->find_all()->as_array(); 
				}
			} 
		
			$dataset = array();
			foreach ($comment_list as $comment){
				$dataset[$comment->id] = array($comment->comment_name,$comment->comment,$comment->male, $comment->female);
			}
			
			return $dataset;
		}
	
		public function get($id){ return ORM::factory("dao_comment")->find($id);}
		public function getAll(){ return ORM::factory("dao_comment")->find_all()->as_array();}
		public function getMale(){return ORM::factory("dao_comment")->where("female","=","0")->where("male","=","1")->find_all()->as_array();	}
		public function getFemale(){return ORM::factory("dao_comment")->where("female","=","1")->where("male","=","0")->find_all()->as_array();}

		//deberia hacer un throw exception por validacion de datos
		public function save($id, array $data){
			$this->comment = ($id > 0) ? ORM::factory("dao_comment",$id) : ORM::factory("dao_comment");
			$this->comment->comment_name = $data['comment_name'];
			$this->comment->comment = $data['comment'];
			$this->comment->female = $data['female'];
			$this->comment->male = $data['male'];
			$this->comment->save();
			return $this->comment->id." guardado con exito";
	    }
		
		public function delete($id){
			$comment = ORM::factory("dao_comment",$id);
			$comment->delete();
			return "Comentario ".$id." eliminado con exito";
		}
			
	}
?>